package com.twu.biblioteca.service;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.LibraryService;
import com.twu.biblioteca.model.ServiceResult;

import java.util.Map;

/**
 * Created by davecroman on 12/09/2014.
 */
public class DisplayBooksService extends LibraryService {
    private final String name = "List Books";

    public DisplayBooksService(Library library) {
        super(library);
    }

    @Override
    public ServiceResult performService() {
        final Map<Integer, Book> allBooks = library.getAllBooks();
        StringBuilder result = new StringBuilder();

        result.append("List of Available Books:\n");
        result.append("======================================================================\n");
        result.append(String.format("%-3s|%-25s | %-25s | %-25s\n", "ID", "Title", "Author", "Publisher"));
        result.append("======================================================================\n");
        for(Book book : allBooks.values()){
            if( book.isAvailable() ){
                result.append(book + "\n");
            }
        }

        return new ServiceResult(result.toString());
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }

}
